package com.eyered.covereasydesign;

import java.io.IOException;
import java.net.MalformedURLException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;

public class LoginActivity extends Activity implements
		android.view.View.OnClickListener {
	ImageButton btnLoginFB;
	Facebook fb;
	String[] permissions = new String[] { "email", "publish_stream" };

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_login);
		String app_id = getString(R.string.app_id);
		fb = new Facebook(app_id);
		SharedPreferences sp = getPreferences(MODE_PRIVATE);
		String token = sp.getString("access_token", null);
		long exprise = sp.getLong("access_exprise", 0);
		if (token != null) {
			fb.setAccessToken(token);
		}
		if (exprise != 0) {
			fb.setAccessExpires(exprise);
			Intent mainItent = new Intent(LoginActivity.this,
					MainActivity.class);
			startActivity(mainItent);
		}
		btnLoginFB = (ImageButton) findViewById(R.id.btnLogin);
		btnLoginFB.setOnClickListener(this);
	}

	@SuppressWarnings("deprecation")
	@Override
	public void onClick(View v) {

		// TODO Auto-generated method stub
		if (!fb.isSessionValid()) {
			fb.authorize(LoginActivity.this, permissions,
					Facebook.FORCE_DIALOG_AUTH, new DialogListener() {

						@Override
						public void onFacebookError(FacebookError e) {
							Log.e("fberrorrrrrrrrrr", e.getMessage());
							Toast.makeText(LoginActivity.this,
									"Test: FB ERROR :" + e.getMessage(),
									Toast.LENGTH_SHORT).show();

						}

						@Override
						public void onError(DialogError e) {
							Log.e("errorrrrrrrrrr", e.getMessage());
							Toast.makeText(LoginActivity.this,
									"Test: ON ERROR :" + e.getMessage(),
									Toast.LENGTH_SHORT).show();

						}

						@Override
						public void onComplete(Bundle values) {

							JSONObject json;
							String userId = null;
							try {
								json = Util.parseJson(fb.request("me"));
								userId = json.getString("id");
							} catch (FacebookError e) {
								// TODO Auto-generated catch block
								Log.e(" Request Me: FB error", e.getMessage());
							} catch (MalformedURLException e) {
								// TODO Auto-generated catch block
								Log.e(" Request Me: MalformedURL",
										e.getMessage());
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								Log.e(" Request Me: JSON", e.getMessage());
							} catch (IOException e) {
								// TODO Auto-generated catch block
								Log.e(" Request Me: IO", e.getMessage());
							}

							SharedPreferences sp = getPreferences(MODE_PRIVATE);
							Editor editor = sp.edit();
							editor.putString("access_token",
									fb.getAccessToken());
							editor.putLong("access_exprise",
									fb.getAccessExpires());
							editor.putString("user_id", userId);
							editor.commit();
							Intent mainItent = new Intent(LoginActivity.this,
									MainActivity.class);
							mainItent.putExtra("user_id", userId);
							startActivity(mainItent);
						}

						@Override
						public void onCancel() {
							Toast.makeText(LoginActivity.this,
									"Test: ON CACEL :", Toast.LENGTH_SHORT)
									.show();

						}
					});
		}
	}

	@SuppressWarnings("deprecation")
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		fb.authorizeCallback(requestCode, resultCode, data);
	}

}
